package com.sg.business.management.handler;

import java.util.List;
import java.util.Map;

import org.eclipse.core.commands.Command;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IWorkbenchPart;

import com.mobnut.db.model.DataSet;
import com.mobnut.db.model.IContext;
import com.mobnut.db.model.PrimaryObject;
import com.sg.business.model.DummyBPMPackage;
import com.sg.business.model.DummyBPMProcess;
import com.sg.business.model.WFHumanTaskSetting;
import com.sg.widgets.command.AbstractNavigatorHandler;
import com.sg.widgets.part.CurrentAccountContext;
import com.sg.widgets.viewer.ViewerControl;

public class SaveTaskSettingHandler extends AbstractNavigatorHandler {

	private IContext context;

	@SuppressWarnings("rawtypes")
	@Override
	protected void execute(PrimaryObject selected, IWorkbenchPart part,
			ViewerControl vc, Command command, Map<String, Object> parameters,
			IStructuredSelection selection) {
		DataSet dataSet = vc.getDataSet();
		context = new CurrentAccountContext();
		List<PrimaryObject> dataItems = dataSet.getDataItems();
		if (dataItems != null) {
			for (int i = 0; i < dataItems.size(); i++) {
				PrimaryObject bpmPackage = dataItems.get(i);
				Object value = bpmPackage
						.getValue(DummyBPMPackage.CHILD_PROCESS);
				if (value != null && value instanceof List) {
					List bps = (List) value;
					for (int j = 0; j < bps.size(); j++) {
						DummyBPMProcess dummyBPMProcess = (DummyBPMProcess) bps
								.get(j);
						Object value2 = dummyBPMProcess
								.getValue(DummyBPMProcess.CHILD_TASK);
						if (value2 != null && value2 instanceof List) {
							List tasks = (List) value2;
							for (int k = 0; k < tasks.size(); k++) {
								WFHumanTaskSetting humanTaskSetting = (WFHumanTaskSetting) tasks
										.get(k);
								try {
									humanTaskSetting.doSave(context);
								} catch (Exception e) {
									e.printStackTrace();
								}
							}
						}
					}
				}
			}
		}
	}

}
